package com.maple.controller.user;


import com.alipay.api.domain.Address;
import com.maple.entity.AddressBook;
import com.maple.result.Result;
import com.maple.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}", addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }


    @GetMapping("/list")
    public Result list(){
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }


    @GetMapping("/{id}")
    public Result queryById(@PathVariable String id){
        log.info("根据id查询地址");
        AddressBook addressBook = addressBookService.queryById(id);
        return Result.success(addressBook);
    }

    @GetMapping("/default")
    public Result queryDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.queryDefault();
        return Result.success(addressBook);
    }


    @PutMapping
    public Result updateById(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址");
        addressBookService.updateById(addressBook);
        return Result.success();
    }


    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}", addressBook.getId());
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }


    @DeleteMapping
    public Result deleteById(@RequestParam String id){
        log.info("根据id删除地址");
        addressBookService.deleteById(id);
        return Result.success();
    }

}
